package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.base.domain.Customer;
import cn.wolfcode.car.business.domain.BusCustomer;
import cn.wolfcode.car.business.mapper.BusCustomerMapper;
import cn.wolfcode.car.business.query.BusCustomerQuery;
import cn.wolfcode.car.business.service.IBusCustomerService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BaseException;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.List;


@Service
@Transactional
public class BusCustomerServiceImpl implements IBusCustomerService {

    @Autowired
    private BusCustomerMapper busCustomerMapper;



    @Override
    public TablePageInfo<BusCustomer> query(BusCustomerQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<BusCustomer>(busCustomerMapper.selectForList(qo));
    }


    @Override
    public void save(BusCustomer busCustomer) {
        this.checkPhoneNumber(busCustomer.getPhone());
        busCustomerMapper.insert(busCustomer);


    }
    public void checkPhoneNumber(String phone) {
        int count  = busCustomerMapper.checkPhoneNumber(phone);
        if (count >0) {
           throw new BusinessException("号码已存在");
        }
    }

    @Override
    public void update(BusCustomer busCustomer) {
        busCustomerMapper.updateByPrimaryKey(busCustomer);
    }

    @Override
    public void deleteBatch(Long id) {
            busCustomerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<BusCustomer> list() {
        return null;
    }

    @Override
    public BusCustomer get(Long id) {
        return busCustomerMapper.selectByPrimaryKey(id);
    }

    @Override
    public Workbook export() {

// 查询出所有权限
        List<BusCustomer> customers = busCustomerMapper.selectAll();
        // 创建工作簿
        Workbook wb = new XSSFWorkbook();
        // 创建页
        Sheet sheet = wb.createSheet("客户列表");
        // 创建标题头
        // 第一行
        Row row = sheet.createRow(0);
        // 第一行第一列，并赋值
        row.createCell(0).setCellValue("编号");
        // 第一行第二列，并赋值
        row.createCell(1).setCellValue("姓名");
        // 第一行第三列，并赋值
        row.createCell(2).setCellValue("联系方式");
        row.createCell(3).setCellValue("生日");
        row.createCell(4).setCellValue("是否vip");
        row.createCell(5).setCellValue("备注信息");
        // 遍历 permissions
        for (int i = 0; i < customers.size(); i++) {
            // 从第二行开始写入内容
            row = sheet.createRow(i + 1);
            BusCustomer busCustomer = customers.get(i);
            row.createCell(0).setCellValue(busCustomer.getId());
            row.createCell(1).setCellValue(busCustomer.getName());
            row.createCell(2).setCellValue(busCustomer.getPhone());
            row.createCell(3).setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(busCustomer.getBirthday()));
            if (busCustomer.getVip() == 0) {
                row.createCell(4).setCellValue("否");
            } else {
                row.createCell(4).setCellValue("是");
            }

            row.createCell(5).setCellValue(busCustomer.getRemark());
        }
        return wb;
    }
}
